package com.itheima.handler;

import com.itheima.exception.BusinessException;
import com.itheima.exception.LoginException;
import com.itheima.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException ex){
        log.warn("系统出现业务异常,异常信息为:{}",ex.getMessage());
        return Result.error(ex.getMessage());
    }

    @ExceptionHandler(LoginException.class)
    public Result handleLoginException(LoginException ex){
        return Result.error(401,null);
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception ex){
        ex.printStackTrace();
        log.error("系统出现异常,导致的原因为:{},异常信息为:{}",ex.getCause(),ex.getMessage());
        return Result.error("系统出现异常,正在紧急修复,请稍后再试");
    }
}
